package com.itheima.app.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
//冻结发布评论
public class FreezePublisInterceptor implements HandlerInterceptor {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Long id = UserHolder.get().getId();
        //接收一下冻结原因
        String freezeReason = null;
        //获取冻结原因
        if(stringRedisTemplate.hasKey(id+"freezeReason")){
            freezeReason = stringRedisTemplate.opsForValue().get(id+"freezeReason");

        }
        //如果redis有这个key，不允许发表评论
        if(stringRedisTemplate.hasKey(id+"publishCommentState")){
            throw new RuntimeException("您的账号因"+freezeReason+"已冻结，请大喊一声勇敢牛牛，不怕困难！");
        }
        return true;
    }
}
